#include "toast.h"
#include <QApplication>
#include <QScreen>
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>

Toast::Toast(const QString &text)
{
    // 设置基本属性
    this->setFixedSize(500, 100);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color: rgb(240, 240, 240); }");
    this->setWindowFlags(Qt::FramelessWindowHint);

    // 设置窗口位置
    // 获取屏幕尺寸
    QScreen *screen = QApplication::primaryScreen();
    int width = screen->size().width();
    int height = screen->size().height();
    int x = (width - this->width()) / 2;
    int y = height - this->height() - 100;
    this->move(x, y);

    //添加布局管理器
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(layout);

    // 创建label
    QLabel *label = new QLabel();
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("QLabel { font-size: 25px; }");
    label->setText(text);
    layout->addWidget(label);

    // 两秒后关闭
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [=](){
        timer->stop();
        this->close();
    });
    timer->start(2000);
}

void Toast::showMessage(const QString &test)
{
    Toast *toast = new Toast(test);
    toast->show();
}
